package com.bos.web.action;

import com.alibaba.fastjson.JSONObject;
import com.bos.web.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.io.FileUtils;
import org.apache.poi.util.SystemOutLogger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.expression.TypeComparator;
import org.springframework.stereotype.Controller;

import javax.servlet.Servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author misterWei
 * @create 2018年11月19号:23点11分
 * @mailbox mynameisweiyan@gmail.com
 */
@ParentPackage("json-default")
@Actions
@Controller
@Namespace("/")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ImageUploadAction extends BaseAction<Object> {
    //文件上传
    private File imgFile;

    private String imgFileFileName;

    private String imgFileContentType;

    public void setImgFile(File imgFile) {
        this.imgFile = imgFile;
    }

    public void setImgFileFileName(String imgFileFileName) {
        this.imgFileFileName = imgFileFileName;
    }

    public void setImgFileContentType(String imgFileContentType) {
        this.imgFileContentType = imgFileContentType;
    }

    @Action(value = "image_upload",results = {@Result(type = "json")})
    public String imageUpload(){
        //imagesUpload  存储上传的图片
        //1,创建相对于当前工程的路径
        String present = ServletActionContext.getRequest().getContextPath() + "/imagesUpload/";
        //指定文件夹的绝对路径
        String realPath = ServletActionContext.getServletContext().getRealPath("/imagesUpload/");
        System.out.println(realPath);
        System.out.println(present+"---");
        //生成图片新名称
        UUID uuid = UUID.randomUUID();
        String substring = imgFileFileName.substring(imgFileFileName.lastIndexOf("."));
        Map<String,Object> resultImagePath = new HashMap<String,Object>(10);


        try {                // error       url
            //3,返回值需要     0 代表成功  还有图片地址值
            File file = new File(realPath + "/" + uuid.toString() + substring);
            FileUtils.copyFile(imgFile,file);
            System.out.println(present);
            resultImagePath.put("error",0);
            resultImagePath.put("url","../.."+present+uuid.toString()+substring);
            ActionContext.getContext().getValueStack().push(resultImagePath);
        } catch (IOException e) {
            resultImagePath.put("error",1);
            resultImagePath.put("url",e.getMessage());
            e.printStackTrace();
        }
        //              1 代表失败  返回的是错误
        System.out.println(imgFileContentType);
        System.out.println(imgFileFileName);
        return SUCCESS;
    }
    @Action(value = "image_manage",results = {@Result(type = "json")})
    public String imageManage () {
        String realPath = ServletActionContext.getServletContext().getRealPath("/imagesUpload/");
        File directory = new File(realPath);
        List<Map<String, Object>> fileList = new ArrayList<>();
        if (directory.listFiles() != null) {
            for (File file : directory.listFiles()) {
                Map<String, Object> hash = new HashMap<>();
                String fileName = file.getName();
                if (file.isDirectory()) {
                    hash.put("is_dir", true);
                    hash.put("has_file", (file.listFiles() != null));
                    hash.put("filesize", 0L);
                    hash.put("is_photo", false);
                    hash.put("filetype", "");
                } else {
                    String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                    hash.put("is_dir", false);
                    hash.put("has_file", false);
                    hash.put("filesize", file.length());
                    hash.put("is_photo", true);
                    hash.put("filetype", fileExt);
                }
                hash.put("filename", fileName);
                long l = file.lastModified();
                String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(l));
                System.out.println(format);
                hash.put("datetime", format);
                fileList.add(hash);
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("moveup_dir_path", "");
        result.put("current_dir_path", realPath);
        result.put("current_url", ServletActionContext.getRequest().getContextPath() + "/imagesUpload/");
        result.put("total_count", fileList.size());
        result.put("file_list", fileList);

        ActionContext.getContext().getValueStack().push(result);

        return SUCCESS;
    }

}
